/*
 * test_all_new_nodes.cpp
 *
 *  Created on: Oct 28, 2012
 *      Author: Federico Orquera
 */

#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include "../bplusfilehandler/newMockFile.h"

NodeID_t GenerateID(){
	static NodeID_t c_ID = 1;
	return c_ID++;
}

int main(){

	newMockFile  	*handler = new newMockFile("NotAFile.mock");

	BPTkey_t 	test1 ("Hola"),
			test2 ("Mundo"),
			test3 ("Zorro"),
			test4 ("Aurora"),
			test5 ("Perro"),
			test6 ("Matias"),
			test7 ("Pure"),
			test8 ("Ponies"),
			test9 ("Mango"),
			test10("Moneda"),
			test11("Aruba"),
			test12("Andorra"),
			test13("Atencion"),
			test14("Arisco"),
			test15("Marisoco"),
			test16("Persona"),
			test17("Pureza"),
			test18("Vaso"),
			test19("sopa"),
			test20("personal"),
			test21("billete");

	LeafRegister_ptr 	testPtr1 (new LeafRegister_t(test1,  test2)),
					testPtr2 (new LeafRegister_t(test2,  test3)),
					testPtr3 (new LeafRegister_t(test3,  test4)),
					testPtr4 (new LeafRegister_t(test4,  test5)),
					testPtr5 (new LeafRegister_t(test5,  test6)),
					testPtr6 (new LeafRegister_t(test6,  test7)),
					testPtr7 (new LeafRegister_t(test7,  test8)),
					testPtr8 (new LeafRegister_t(test8,  test9)),
					testPtr9 (new LeafRegister_t(test9,  test10)),
					testPtr10(new LeafRegister_t(test10, test11)),
					testPtr11(new LeafRegister_t(test11, test12)),
					testPtr12(new LeafRegister_t(test12, test13)),
					testPtr13(new LeafRegister_t(test13, test14)),
					testPtr14(new LeafRegister_t(test14, test15)),
					testPtr15(new LeafRegister_t(test15, test16)),
					testPtr16(new LeafRegister_t(test16, test17)),
					testPtr17(new LeafRegister_t(test17, test18)),
					testPtr18(new LeafRegister_t(test18, test19)),
					testPtr19(new LeafRegister_t(test19, test20)),
					testPtr20(new LeafRegister_t(test20, test21)),
					testPtr21(new LeafRegister_t(test21, test1));

	node_ptr 		testNodePtr1, testNodePtr2;

	Leaf 			testLeaf1(ORDER,GenerateID(),isOverflowLeaf_func,isUnderflowLeaf_func),
					testLeaf2(ORDER,GenerateID(),isOverflowLeaf_func,isUnderflowLeaf_func);

	Internal 		testinternal1(ORDER,2,0,isOverflowInternal_func,isUnderflowInternal_func,handler);

	testLeaf1.setRightSibling(testLeaf2.getNodeID());

	testNodePtr1 = handler->getNewNode(0);
	testNodePtr2 = handler->getNewNode(1);

	((Internal*)testNodePtr2.get())->setFirstSon(testNodePtr1->getNodeID());

	testinternal1.setFirstSon(testNodePtr2->getNodeID());

	cout<<endl<<"Testeando Leaf:"<<endl<<endl;

	cout<<"\t getLevel() "	<<((testLeaf1.getLevel() == 0)? "OK":"FAIL")<<endl;
	cout<<"\t getOrder() "	<<((testLeaf1.getOrder() == ORDER)?"OK":"FAIL")<<endl;
	cout<<"\t getRightSibling() "<<((testLeaf1.getRightSibling() == testLeaf2.getNodeID())?"OK":"FAIL")<<endl;
	testLeaf1.InsertRegister(testPtr1);
	//cout<<"\t SearchRegister() "<<((testLeaf1.SearchRegister(testPtr1->getKey())->getKey() == testPtr1->getKey())? "OK":"FAIL")<<endl;

	cout<<endl<<"Insertando multiples registros:"<<endl<<endl;
	cout<<"\t InsertRegister("<<testPtr2->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr2)	== UPDATED)?  "OK":"FAIL")<<endl;
	cout<<"\t InsertRegister("<<testPtr3->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr3)	== UPDATED)?  "OK":"FAIL")<<endl;
	cout<<"\t InsertRegister("<<testPtr4->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr4)	== UPDATED)?  "OK":"FAIL")<<endl;
	cout<<"\t InsertRegister("<<testPtr5->getKey()<<") OVERFLOW "<<((testLeaf1.InsertRegister(testPtr5)	== OVERFLOW)? "OK":"FAIL")<<endl;

	cout<<endl<<"Mostrando Contenido:"<<endl<<endl;
	testLeaf1.MostrarContenidoReducido("\t ");

	cout<<endl<<"Eliminando multiples registros:"<<endl<<endl;
	cout<<"\t DeleteRegister("<<testPtr2->getKey()<<") UPDATED "  <<((testLeaf1.DeleteRegister(testPtr2->getKey()) == UPDATED)?   "OK":"FAIL")<<endl;
	cout<<"\t DeleteRegister("<<testPtr3->getKey()<<") UPDATED "  <<((testLeaf1.DeleteRegister(testPtr3->getKey()) == UPDATED)?   "OK":"FAIL")<<endl;
	cout<<"\t DeleteRegister("<<testPtr4->getKey()<<") UPDATED "  <<((testLeaf1.DeleteRegister(testPtr4->getKey()) == UPDATED)?   "OK":"FAIL")<<endl;
	cout<<"\t DeleteRegister("<<testPtr5->getKey()<<") UNDERFLOW "<<((testLeaf1.DeleteRegister(testPtr5->getKey()) == UNDERFLOW)? "OK":"FAIL")<<endl;

	cout<<endl<<"Mostrando Contenido:"<<endl<<endl;
	testLeaf1.MostrarContenidoReducido("\t ");

	cout<<endl<<"Testeando getHalfList():"<<endl<<endl;
	cout<<"\t InsertRegister("<<testPtr2->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr2)	== UPDATED)?  "OK":"FAIL")<<endl;
	cout<<"\t InsertRegister("<<testPtr3->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr3)	== UPDATED)?  "OK":"FAIL")<<endl;
	cout<<"\t InsertRegister("<<testPtr4->getKey()<<") UPDATED " <<((testLeaf1.InsertRegister(testPtr4)	== UPDATED)?  "OK":"FAIL")<<endl;

	LeafRegisterList_t newList = testLeaf1.getHalfList();
	typename LeafRegisterList_t::iterator it;

	cout<<endl<<"Mostrando Contenido newList:"  <<endl<<endl;
	for ( it=newList.begin() ; it != newList.end(); it++ )
		cout <<"\t ("<< (*it)->getKey() << ")"<<endl;
	cout<<endl<<"Mostrando Contenido testLeaf1:"<<endl<<endl;
	testLeaf1.MostrarContenidoReducido("\t ");

	cout<<endl<<"Testeando Internal:"<<endl<<endl;

	cout<<"\t getLevel() "	<<((testinternal1.getLevel()	== 1)? "OK":"FAIL")<<endl;
	cout<<"\t getOrder() "	<<((testinternal1.getOrder() 	== ORDER)?"OK":"FAIL")<<endl;
	cout<<"\t getFirstSon() "	<<((testinternal1.getFirstSon() 	== testNodePtr1->getNodeID())?"OK":"FAIL")<<endl;

	cout<<endl<<"Mostrando Contenido testinternal1:"<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");

	cout<<endl<<"Insertando multiples registros:"<<endl<<endl;
	cout<<"\t InsertRegister("<<testPtr1->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr1)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr2->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr2)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr3->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr3)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr4->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr4)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr5->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr5)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr6->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr6)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr7->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr7)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr8->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr8)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr9->getKey()<<") NO_CHANGE " 	<<((testinternal1.InsertRegister(testPtr9)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr10->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr10)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr11->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr11)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr12->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr12)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr13->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr13)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr14->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr14)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr15->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr15)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr16->getKey()<<") UPDATED " <<((testinternal1.InsertRegister(testPtr16)	== UPDATED  )?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr17->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr17)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr18->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr18)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr19->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr19)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr20->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr20)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");
	cout<<"\t InsertRegister("<<testPtr21->getKey()<<") NO_CHANGE " <<((testinternal1.InsertRegister(testPtr21)	== NO_CHANGE)?  "OK":"FAIL")<<endl<<endl;
	testinternal1.MostrarContenidoReducido("\t ");

	cout<<endl;
	return 0;
}
